﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace QuanLiGiaiBongDa
{
    public partial class frmQlKetQua : Form
    {
        public frmQlKetQua()
        {
            InitializeComponent();
        }
        QuanLyKetQuaTD_BLL qlkq = new QuanLyKetQuaTD_BLL();
        DataSet ds = new DataSet();
        private void frmQlKetQua_Load(object sender, EventArgs e)
        {
            Load_KetQuaThiDau();
        }
        
        /// ///////////////////////////////
        /// //Tab Ket Qua Thi Dau
        public void XoaNhap()
        {
            cbdoibong.SelectedIndex = -1;
            cbMatrandau.SelectedIndex = -1;
            txtsobanthang.Clear();
            txtsothephat.Clear();
            cbkqthidau.SelectedIndex = -1;
        }
        private KetQua GetValue_KetQuaThiDau()
        {
            KetQua kq = new KetQua();
            kq.Ketqua = cbkqthidau.Text;
            kq.Matrd = cbMatrandau.Text;
            kq.Sobt = int.Parse(txtsobanthang.Text);
            kq.Sothe = int.Parse(txtsothephat.Text);
            dataGridViewkqthidau2.DataSource = qlkq.TimMaDBTheoTen(cbdoibong.Text);
            if (dataGridViewkqthidau2[0, 0].Value != null)
                kq.Madb = dataGridViewkqthidau2[1, 1].Value.ToString();
            return kq;
        }
        public void Load_KetQuaThiDau()
        {
            //string LoadKQ = "select TRANdau.MaTrd,TenDB,SoBT,SoThe,KetQua from DOIBONG,KETQUA,TRANDAU where MaDB1= doibong.madb";
            dataGridViewkqthidau.DataSource = qlkq.Load(SQLProvider.GetAll("ketqua"));
            //dataGridViewkqthidau2.DataSource = qlkq.Load("select TRANdau.MaTrd,TenDB,SoBT,SoThe,KetQua from DOIBONG,KETQUA,TRANDAU where MaDB1= doibong.madb");
        }
        private void btthem_Click(object sender, EventArgs e)
        {
            //DBAccess db = new DBAccess();
            //string _MaTD = cbMatrandau.SelectedItem.ToString();
            //string _MaDB = cbdoibong.SelectedItem.ToString();
            //string _SoBT = txtsobanthang.Text;
            //string _SoTP = txtsothephat.Text;
            //string _KQ = cbkqthidau.SelectedItem.ToString();
            //string sql = "insert into doibong values ('" + _MADB + "','" + _TenDB + "','" + _MaSan + "','" + _DiaChi + "',NULL,NULL,NULL)";
            //db.ExecNonQuery(sql);
            //MessageBox.Show("Insert Successful!", "Message", MessageBoxButtons.OK);
            //string sql2 = "select * from doibong";
            //dataGridViewdsdb.DataSource = db.ExecSQLDataTable(sql2);
            //LoadData_DB();
        }
        private void btxoa_Click(object sender, EventArgs e)
        {
            if (!DataUtils.KiemTra(cbdoibong.Text, cbMatrandau.Text))
            {
                MessageBox.Show("Bạn chưa chọn thông tin để xoá");
            }
            else if (DataUtils.KiemTra(cbdoibong.Text, cbMatrandau.Text))
            {
                DialogResult d = MessageBox.Show("Bạn Có Muốn Xóa Không ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                ds = qlkq.GetData("KETQUA");
                MessageBox.Show("Xoá thành công");
                this.Load_KetQuaThiDau();
            }
            else
            {
                MessageBox.Show("Xoá thất bại");
            }
        }
        private void btcapnhap_Click(object sender, EventArgs e)
        {
            if (!DataUtils.KiemTra(cbdoibong.Text, cbMatrandau.Text))
            {
                MessageBox.Show("Bạn chưa chọn thông tin để cập nhật");
            }
            else if (qlkq.Update_KetQua(GetValue_KetQuaThiDau()))
            {
                MessageBox.Show("Cập nhật thành công");
                this.Load_KetQuaThiDau();
            }
            else
            {
                MessageBox.Show("Cập nhật thất bại");
            }
        }
        private void btRefresh_Click(object sender, EventArgs e)
        {
            cbkqthidau.Text = "";
            cbMatrandau.Text = "";
            cbdoibong.Text = "";
            txtsobanthang.Text = "";
            txtsothephat.Text = "";
            cbdoibong.Enabled = true;
            cbMatrandau.Enabled = true;
            this.Load_KetQuaThiDau();
        }
        private void dataGridViewkqthidau_Click(object sender, EventArgs e)
        {
            if (dataGridViewkqthidau.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridViewkqthidau.SelectedRows[0];
                int vitri_row = dataGridViewkqthidau.Rows.IndexOf(row);
                cbMatrandau.Text = dataGridViewkqthidau[0, vitri_row].Value.ToString();
                cbMatrandau.Enabled = false;
                cbdoibong.Text = dataGridViewkqthidau[1, vitri_row].Value.ToString();
                cbdoibong.Enabled = false;
                txtsobanthang.Text = dataGridViewkqthidau[2, vitri_row].Value.ToString();
                txtsothephat.Text = dataGridViewkqthidau[3, vitri_row].Value.ToString();
                cbkqthidau.Text = dataGridViewkqthidau[4, vitri_row].Value.ToString();
            }
        }
        private void cbdoibong_MouseClick(object sender, MouseEventArgs e)
        {
            cbdoibong.Items.Clear();
            qlkq.MaDoiBong_by_MaTranDau_KQ(cbMatrandau,cbdoibong);
        }
        private void txtsobanthang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != '\b')
            {
                MessageBox.Show("Bạn chỉ được nhập số.");
                e.KeyChar = '\0';
            }
        }
        private void cbMatrandau_MouseClick(object sender, MouseEventArgs e)
        {
            cbMatrandau.Items.Clear();
            qlkq.MaTranDau_KQ(cbMatrandau);
        }


        /// ///////////////////////////////
        /// //Tab Danh Sach cau thu ghi ban
        public void Load_CTGhiBan()
        {
            dataGridViewdscauthghiban.DataSource = qlkq.Load(SQLProvider.GetAll("GhiBan"));
        }
        void XoaNhapCT()
        {
            txtmaghiban.Clear();
            cbmatranctghiban.SelectedIndex = -1;
            cbloaibanthang.SelectedIndex = -1;
            cbcauthu.SelectedIndex = -1;
            txtthoidiem.Clear();
        }
        private GhiBan GetValue_CTGhiBan()
        {
            GhiBan gb = new GhiBan();
            gb.Ghichu = cbloaibanthang.Text;
            gb.Mabt = txtmaghiban.Text.ToUpper();
            gb.Matrd = cbmatranctghiban.Text;
            gb.Thoidiem = int.Parse(txtthoidiem.Text);
            dataGridViewdscauthghiban2.DataSource = qlkq.TimMaCTTheoTen(cbcauthu.Text);
            if (dataGridViewdscauthghiban2[0, 0].Value != null)
                gb.Mact = dataGridViewdscauthghiban2[0, 0].Value.ToString();
            return gb;
        }
        private void cbmatranctghiban_MouseClick(object sender, MouseEventArgs e)
        {
            cbcauthu.Text = "";
            cbmatranctghiban.Items.Clear();
            qlkq.MaTranDau_GhiBan(cbmatranctghiban);
        }
        private void cbcauthu_MouseClick(object sender, MouseEventArgs e)
        {
            cbcauthu.Items.Clear();
            qlkq.MaCauThu_by_MaTranDau_GhiBan(cbmatranctghiban, cbcauthu);
        }
        private void btthemdscauthughiban_Click(object sender, EventArgs e)
        {
            if (btthemdscauthughiban.Text.Trim().Equals("Thêm"))
            {
                btthemdscauthughiban.Text = "Hủy";
                txtmaghiban.Enabled = true;
                btxoadscauthughiban.Enabled = false;
                XoaNhapCT();
            }
            else
            {
                btthem.Text = "Thêm";
                txtmaghiban.Enabled = false;
            }
        }
        private void btxoadscauthughiban_Click(object sender, EventArgs e)
        {
            if (!DataUtils.KiemTra(txtmaghiban.Text))
            {
                MessageBox.Show("Bạn chưa chọn thông tin để xoá");
            }
            else if (DataUtils.KiemTra(cbdoibong.Text, cbMatrandau.Text))
            {
                DialogResult d = MessageBox.Show("Bạn Có Muốn Xóa Không ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                qlkq.Delete_GhiBan(GetValue_CTGhiBan());
                MessageBox.Show("Xoá thành công");
                this.Load_CTGhiBan();
            }
            else
            {
                MessageBox.Show("Xoá thất bại");
            }
        }
        private void btcapnhapdscauthughiban_Click(object sender, EventArgs e)
        {
            if (!DataUtils.KiemTra(txtmaghiban.Text))
            {
                MessageBox.Show("Bạn chưa nhập thông tin  cần cập nhật");
            }
            else if (qlkq.Update_GhiBan(GetValue_CTGhiBan()))
            {
                MessageBox.Show("Cập nhật thành công");
                this.Load_CTGhiBan();
            }
            else
            {
                MessageBox.Show("Cập nhật không thành công");
            }
        }
        private void btRefreshdscauthughiban_Click(object sender, EventArgs e)
        {
            txtmaghiban.Text = "";
            cbcauthu.Text = "";
            cbloaibanthang.Text = "";
            cbmatranctghiban.Text = "";
            txtthoidiem.Text = "";
            txtmaghiban.Enabled = true;
            this.Load_CTGhiBan();
        }
        private void dataGridViewdscauthghiban_Click(object sender, EventArgs e)
        {
            if (dataGridViewdscauthghiban.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridViewdscauthghiban.SelectedRows[0];
                int vitri_row = dataGridViewdscauthghiban.Rows.IndexOf(row);
                txtmaghiban.Text = dataGridViewdscauthghiban[0, vitri_row].Value.ToString();
                txtmaghiban.Enabled = false;
                cbmatranctghiban.Text = dataGridViewdscauthghiban[1, vitri_row].Value.ToString();
                cbcauthu.Text = dataGridViewdscauthghiban[2, vitri_row].Value.ToString();
                txtthoidiem.Text = dataGridViewdscauthghiban[3, vitri_row].Value.ToString();
                cbloaibanthang.Text = dataGridViewdscauthghiban[4, vitri_row].Value.ToString();
            }
        }

        /// ///////////////////////////////
        /// //Tab Danh Sach cau thu bi phat
        public void Load_CTPhat()
        {
            dataGridViewcauthubiphat.DataSource = qlkq.Load(SQLProvider.GetAll("Phat"));
        }
        private Phat GetValue_CTPhat()
        {
            Phat p = new Phat();
            p.Loaithe = cbloaithephat.Text;
            p.Maphat = txtmaphat.Text.ToUpper();
            p.Matrd = cbmatrandaubiphat.Text;
            p.Thoidiem = DataUtils.ToInt(txtthoidiembiphat.Text);
            dataGridViewcauthubiphat2.DataSource = qlkq.TimMaCTTheoTen(cbcauthubiphat.Text);
            if (dataGridViewcauthubiphat2[0, 0].Value != null)
                p.Mact = dataGridViewcauthubiphat2[0, 0].Value.ToString();
            return p;
        }
        private void btthemcauthubiphat_Click(object sender, EventArgs e)
        {
            if (!DataUtils.KiemTra(cbloaithephat.Text, cbcauthubiphat.Text, txtmaphat.Text, cbmatrandaubiphat.Text))
            {
                MessageBox.Show("Bạn chưa nhập đầy đủ thông tin");
            }
            else if (qlkq.Insert_Phat(GetValue_CTPhat()))
            {
                MessageBox.Show("Thêm thành công");
                this.Load_CTPhat();
            }
            else
            {
                MessageBox.Show("Bạn nhập mã phạt bị trùng");
            }
        }
        private void btxoacauthubiphat_Click(object sender, EventArgs e)
        {
            if (!DataUtils.KiemTra(txtmaphat.Text))
            {
                MessageBox.Show("Bạn chưa nhập thông tin cần xoá");
            }
            else if (qlkq.Delete_Phat(GetValue_CTPhat()))
            {
                MessageBox.Show("Xoá thành công");
                this.Load_CTPhat();
            }
            else
            {
                MessageBox.Show("Xoá không thành công");
            }

        }
        private void btcapnhapcauthubiphat_Click(object sender, EventArgs e)
        {
            if (!DataUtils.KiemTra(txtmaphat.Text))
            {
                MessageBox.Show("Bạn chưa nhập thông tin cần cập nhật");
            }
            else if (qlkq.Update_Phat(GetValue_CTPhat()))
            {
                MessageBox.Show("Cập nhật thành công");
                this.Load_CTPhat();
            }
            else
            {
                MessageBox.Show("Cập nhật không thành công");
            }
        }
        private void btRefreshcauthubiphat_Click(object sender, EventArgs e)
        {
            txtmaphat.Text = "";
            cbmatrandaubiphat.Text = "";
            cbcauthubiphat.Text = "";
            cbloaithephat.Text = "";
            txtthoidiembiphat.Text = "";
            txtmaphat.Enabled = true;
            this.Load_CTPhat();
        }
        private void dataGridViewcauthubiphat_Click(object sender, EventArgs e)
        {
            if (dataGridViewcauthubiphat.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridViewcauthubiphat.SelectedRows[0];
                int vitri_row = dataGridViewcauthubiphat.Rows.IndexOf(row);
                txtmaphat.Text = dataGridViewcauthubiphat[0, vitri_row].Value.ToString();
                txtmaphat.Enabled = false;
                cbmatrandaubiphat.Text = dataGridViewcauthubiphat[1, vitri_row].Value.ToString();
                cbcauthubiphat.Text = dataGridViewcauthubiphat[2, vitri_row].Value.ToString();
                txtthoidiembiphat.Text = dataGridViewcauthubiphat[3, vitri_row].Value.ToString();
                cbloaithephat.Text = dataGridViewcauthubiphat[4, vitri_row].Value.ToString();
            }
        }
        private void cbmatrandaubiphat_MouseClick(object sender, MouseEventArgs e)
        {
            cbmatrandaubiphat.Text = "";
            cbmatrandaubiphat.Items.Clear();
            qlkq.MaTranDau_Phat(cbmatrandaubiphat);
        }
        private void cbcauthubiphat_MouseClick(object sender, MouseEventArgs e)
        {
            cbcauthubiphat.Items.Clear();
            qlkq.MaCauThu_by_MaTranDau_Phat(cbmatrandaubiphat, cbcauthubiphat);
        }
        private void cbmatrandaubiphat_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = '\0';
        }

        /// ///////////////////////////////
        /// //Tab Xep Hang
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 1)
            {
                this.Load_CTGhiBan();
            }
            if (e.TabPageIndex == 2)
            {
                this.Load_CTPhat();
            }

        }


        // /////////////////
        // /////////////////////////////
        private void frmQlKetQua_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult d = MessageBox.Show("Bạn muốn đóng Quản lý kết quả?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (d == DialogResult.No)
                e.Cancel = true;
        }

       

       

      

    }
}
